﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebService_Newstore.DAO
{
    public class UniteDAO : BaseDAO
    {
        public int id;
        public string code;                        // ex: KG
        public string libelle;                     // ex: Kilogramme

        public UniteDAO(int idUnite)
        {
            entite = new newStoreEntities();
            string req = "SELECT * FROM unite WHERE id_unite = " + idUnite;

            try
            {
                var userQuery = from unite in newStoreContext.unites
                                where unite.id_unite == idUnite
                                select unite;

                if (userQuery.LongCount() == 0)
                {
                    //On instancie un DAO vide pour faire remonter l'erreur au niveau du controller
                    this.id = 0;
                }

                foreach (var ligne in userQuery)
                {
                    //On instancie l'unité
                    this.id = ligne.id_unite;
                    this.code = ligne.nom_unite;
                    this.libelle = ligne.val_unite;

                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }

}
